package main.java.br.com.projeto.domain.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import main.java.br.com.projeto.domain.entity.Funcionario;

public class DaoFuncionario extends Dao<Funcionario>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 966952189106917028L;

	public DaoFuncionario() {
		super(Funcionario.class);
	}
	
	public List<Funcionario> listByFilter(Long id, String filter, String filterTrab) {
		String hql = "select funcionario from Funcionario funcionario "
				+ "where (lower(funcionario.nome) like '%' || lower ( cast(:filter as string) ) || '%' or :filter = null) "
				+ "and (funcionario.id = :id or :id = 0) "
				+ "and (funcionario.carteiraTrabalho like '%' || cast(:filterTrab  as string) || '%' or :filterTrab = null)";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("filter", filter);
		parameters.put("id", id);
		parameters.put("filterTrab", filterTrab);

		return this.findOneResult(hql, parameters);
	}
	
	public List<Funcionario> listFuncionariosServicos (Long servicoId) {
		String hql = "select funcionario from Funcionario funcionario "
				+ "join funcionario.servicos servicos "
				+ "where servicos.id = :servicoId";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("servicoId", servicoId);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Funcionario> listByCpf( String cpf )
	{
		String hql = "select funcionario from Funcionario funcionario "
				+ "where funcionario.cpf = cast(:cpf as string) ";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("cpf", cpf);

		return this.findOneResult(hql, parameters);
	}
	
	public List<Funcionario> listFuncionariosServicosByQuery( Long servicoId, String query )
	{
		String hql = "select funcionario from Funcionario funcionario "
				+ "join funcionario.servicos servicos "
				+ "where servicos.id = :servicoId "
				+ "and (lower(funcionario.nome) like '%' || lower ( cast(:query as string) ) || '%')";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("servicoId", servicoId);
		parameters.put("query", query);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Funcionario> listFuncionariosByQuery(  String query )
	{
		String hql = "select funcionario from Funcionario funcionario "
				+ "where (lower(funcionario.nome) like '%' || lower ( cast(:query as string) ) || '%')";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("query", query);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Funcionario> listByFilterAndServico( Long id, String filter, Long servicoId ) 
	{
		String hql = "select funcionario from Funcionario funcionario "
				+ "join funcionario.servicos servicos "
				+ "where (lower(funcionario.nome) like '%' || lower ( cast(:filter as string) ) || '%' or :filter = null) "
				+ "and (funcionario.id = :id or :id = 0) "
				+ "and (servicos.id = :servicoId)";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("filter", filter);
		parameters.put("id", id);
		parameters.put("servicoId", servicoId);
		
		return this.findOneResult(hql, parameters);
	}
}
